package main

import (
	"github.com/streadway/amqp"
)

type RabbitMqQueueWriter struct {
	ch        *amqp.Channel
	queueName string
}

func NewRabbitMqWriter(queueName string, ch *amqp.Channel) *RabbitMqQueueWriter {
	return &RabbitMqQueueWriter{
		ch:        ch,
		queueName: queueName,
	}
}

func (w *RabbitMqQueueWriter) Write(p []byte) (int, error) {
	err := w.ch.Publish(
		w.queueName, // exchange
		"",          // routing key
		false,       // mandatory
		false,       // immediate
		amqp.Publishing{
			ContentType: "text/plain",
			Body:        p,
		})
	return 1, err
}

/*func main2()  {
	conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/")
	failOnError(err, "Failed to connect to RabbitMQ")
	defer conn.Close()

	ch, err := conn.Channel()
	failOnError(err, "Failed to open a channel")
	defer ch.Close()

	q, err := ch.QueueDeclare(
		"hello", // name
		false,   // durable
		false,   // delete when unused
		false,   // exclusive
		false,   // no-wait
		nil,     // arguments
	)
	failOnError(err, "Failed to declare a queue")

	body := "Hello World!"
	err = ch.Publish(
		"",     // exchange
		q.Name, // routing key
		false,  // mandatory
		false,  // immediate
		amqp.Publishing{
			ContentType: "text/plain",
			Body:        []byte(body),
		})
	failOnError(err, "Failed to publish a message")
	log.Printf(" [x] Sent %s", body)
}*/
